package br.com.californiahotel.client.viewControler.servico;

import br.com.californiahotel.client.architecture.viewController.BasicView;
import br.com.californiahotel.client.viewControler.servico.ServicoFormController.Display;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FloatItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class ServicoFormView extends BasicView implements Display {

	public enum Field{
		DESCRIPTION("Descricao"),VALUE("valor"),SAVE("PasswordField"),RESET("reset"),HIDDEN("hidden");

		private final String fieldName;

		private Field(String name) {
			fieldName = name;
		}

		public String getFieldName() {
			return fieldName;
		}

	}

	private DynamicForm form;

	public ServicoFormView() {
		mountForm();
		setAutoWidth();
		setHeight100();
		setMargin(20);
	}

	public ServicoFormView(Long cdServico, String description, Double value) {
		mountForm();
		setAutoWidth();
		setHeight100();
		setMargin(20);
		form.getItem(Field.HIDDEN.getFieldName()).setValue(cdServico);
		form.getItem(Field.DESCRIPTION.getFieldName()).setValue(description);
		form.getItem(Field.VALUE.getFieldName()).setValue(value.toString().replace(".", ","));
	}

	private void mountForm() {
		form = new DynamicForm();
		form.setShowEdges(true);

		HiddenItem hiddenItem = new HiddenItem(Field.HIDDEN.getFieldName());

		TextItem textItem = new TextItem(Field.DESCRIPTION.getFieldName(), "Descri\u00E7\u00E3o");
		FloatItem floatItem = new FloatItem(Field.VALUE.getFieldName(),"Valor");
		floatItem.setKeyPressFilter("[0-9,]");

		ButtonItem buttonItem = new ButtonItem(Field.SAVE.getFieldName(), "Salvar");
		buttonItem.setAlign(Alignment.RIGHT);
		buttonItem.setEndRow(false);
		buttonItem.setStartRow(false);

		form.setCellPadding(5);
		form.setFields(textItem, floatItem, new SpacerItem(), buttonItem, hiddenItem);
		addChild(form);
	}

	@Override
	public DynamicForm getForm() {
		return form;
	}

}
